import os, optparse, binascii

def mp3codec(filename):

    print filename

    f = open(filename,'rb')
    mp3data = f.read()
    f.close()

    frame = binascii.unhexlify('0000')
    print "****"
    print frame
    print "****"

    for ii in range(len(mp3data)-1):
        if (ord(mp3data[ii]) == 255):
            if ((ord(mp3data[ii+1]) & 192) == 192):
                frameHeader = mp3data[ii:ii+4]
                readHeader(frameHeader)

def readHeader(frameHeader):

    frameSync = ((ord(frameHeader[0]) & 255)<<2) + (ord(frameHeader[1]) & 192)

    version = ord(frameHeader[1]) & 24
    layerDescription = ord(frameHeader[1]) & 6
    protectionBit = ord(frameHeader[1]) & 1

    bitrateIndex = ord(frameHeader[2]) & 240
    samplingRateFreqIndex = ord(frameHeader[2]) & 12
    paddingBit = ord(frameHeader[2]) & 2
    privateBit = ord(frameHeader[2]) & 1

    channelMode = ord(frameHeader[3]) & 192
    modeExtension = ord(frameHeader[3]) & 48
    copyright = ord(frameHeader[3]) & 8
    original = ord(frameHeader[3]) & 4
    emphasis = ord(frameHeader[3]) & 3

    print "Frame Sync: %s" % str(frameSync)
    print "Version: %s" % str(version)

if __name__ == "__main__": 
    parser = optparse.OptionParser()
    opts,args = parser.parse_args()
    mp3codec(args[0])
